package cn.ljy.authority.web.controller.backstage.log;


import cn.ljy.authority.service.SysWechatUserService;
import cn.ljy.authority.utils.JwtUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "微信小程序授权")
@RestController
@RequestMapping("/backstage/wechat/user")
public class SysWechatUserController {

    @Autowired
    private SysWechatUserService wechatUserService;

    @Autowired
    private JwtUtils jwtUtils;

  /*  @GetMapping("/info")
    public ResponseDTO<SysWechatUser> getWechatUserInfo(@RequestHeader("Authorization") String token) {
        try {
            Long userId = jwtUtils.getUserIdFromToken(token.replace("Bearer ", ""));
            if (userId == null) {
                throw new BusinessException("token无效");
            }

            SysWechatUser wechatUser = wechatUserService.getWechatUserByUserId(userId);
            return ResponseDTO.success(wechatUser);
        } catch (Exception e) {
            throw new BusinessException("获取微信用户信息失败");
        }
    }

    @PostMapping("/unbind")
    public ResponseDTO<?> unbindWechat(@RequestHeader("Authorization") String token) {
        try {
            Long userId = jwtUtils.getUserIdFromToken(token.replace("Bearer ", ""));
            if (userId == null) {
                throw new BusinessException("token无效");
            }

            boolean success = wechatUserService.unbindWechatUser(userId);
            return success ? ResponseDTO.success("解绑成功") : ResponseDTO.error("解绑失败");
        } catch (Exception e) {
            throw new BusinessException("解绑失败: " + e.getMessage());
        }
    }

    @PutMapping("/info")
    public ResponseDTO<?> updateWechatUserInfo(@RequestHeader("Authorization") String token,
                                               @RequestBody SysWechatUser wechatUser) {
        try {
            Long userId = jwtUtils.getUserIdFromToken(token.replace("Bearer ", ""));
            if (userId == null) {
                throw new BusinessException("token无效");
            }

            wechatUser.setUserId(userId);
            boolean success = wechatUserService.updateWechatUserInfo(wechatUser);
            return success ? ResponseDTO.success("更新成功") : ResponseDTO.error("更新失败");
        } catch (Exception e) {
            throw new BusinessException("更新失败: " + e.getMessage());
        }
    }*/
}